$(document).ready(function () {

    /*校验验证码*/
    var verifyCode = new GVerify("v_container");
    function checkVerifyCode() {
        var res = verifyCode.validate(document.getElementById("code_input").value);
        return res;
    }


    //$("#error").hide();
    //$("#success").hide();
    $("#userNum").attr("value", getUserNum());


    var identity;
    /**
     * input失去焦点时获取用户身份
     *地址：http://39.108.233.68:8002/returnUserType?user_num=1001
     */
    $("#showIdentity #userNum").blur(function () {

        var userNum = this.value;
        window.localStorage.setItem("userNum", userNum);
        //alert(this.value);
        //console.log($.cookie("userNum"));
        if (userNum) {

            var showIdentity = new Vue({
                el: "#showIdentity",
                data: {
                    showIdentity: {}
                },
                created: function() {
                    this.getIdentity();
                },
                methods: {
                    getIdentity: function () {
                        var that = this;
                        axios.get("/returnUserType?userNum=" + getUserNum()).then(
                            function (response) {
                                console.log(response);
                                if (response.data.code === 500) {
                                    console.log("账户输入错误！");
                                    // 输入框变成红色
                                    $("#showIdentity").addClass("has-error");
                                    $("#userNum").attr("aria-describedby", "error");
                                    $("#error").show();
                                    alert("用户账户错误，请重新输入");
                                    window.location.href = "front_login.html";
                                } else if (response.data.code === 200) {

                                    identity = response.data.data;
                                    console.log("身份码");
                                    console.log(identity);
                                    if ($("#showIdentity").hasClass("has-error")) {
                                        $("#error").hide();
                                        $("#showIdentity").removeClass("has-error");
                                    }
                                    // 输入框变成绿色
                                    $("#showIdentity").addClass("has-success");
                                    $("#userNum").attr("aria-describedby", "success");
                                    $("#success").show();
                                    console.log(userNum);
                                    $.cookie("userNum", userNum);

                                    $("#userNum").attr("value", userNum);



                                    /*判断身份*/
                                    $("#success").text("");
                                    if (response.data.data === 3)
                                        $("#success").append("欢迎管理员登录");
                                    else if (response.data.data === 1)
                                        $("#success").append("欢迎学生登录");
                                    else
                                        $("#success").append("欢迎老师登录");

                                    /*判断密码输入是否正确*/
                                    var checkPwd;
                                    $("#login1").on("click", function () {
                                        var pwd = document.getElementById("password").value;
                                        var password = new Vue({
                                            el: "#password",
                                            data: {
                                                //checkPassword: {}
                                            },
                                            created: function() {
                                                this.checkPwd();
                                            },
                                            methods: {
                                                checkPwd: function () {
                                                    var that = this;
                                                    axios.get("/singleLogin?userNum=" + getUserNum() +"&password=" + pwd).then(
                                                        function (response) {
                                                            //alert(response.data.code);
                                                            checkPwd = response.data.code===200?true:false;
                                                            //$.cookie("checkPassword", that.checkPassword);
                                                            //return response.code === 200;
                                                            $("#password").attr("value", pwd);

                                                            $("#success1").text("");
                                                            $("#error1").text("");
                                                            $("#success2").text("");
                                                            $("#error2").text("");
                                                            $("#showPassword").removeClass("has-error");
                                                            $("#showVerify").removeClass("has-error");

                                                            /*登录成功*/
                                                            if (checkVerifyCode() && checkPwd) {
                                                                //alert("页面跳转");
                                                                $.cookie("userNum", getUserNum());
                                                                //alert($.cookie("userNum"));
                                                                if (identity === 3)
                                                                    document.location.href = "admin_index.html";
                                                                if (identity === 1)
                                                                    document.location.href = "info.html";
                                                                if (identity === 2)
                                                                    document.location.href = "all-admin-index.html";
                                                            }
                                                            else if (!checkPwd && checkVerifyCode()) {
                                                                //密码错误
                                                                $("#showPassword").addClass("has-error");
                                                                $("#password").attr("aria-describedby", "error");
                                                                $("#error1").show();
                                                                $("#error1").text("");
                                                                $("#error1").append("密码输入错误");
                                                                //验证码正确
                                                                $("#showVerify").addClass("has-success");
                                                                $("#code_input").attr("aria-describedby", "success");
                                                                $("#success2").show();
                                                                $("#success2").text("");
                                                                $("#success2").append("验证码输入正确");

                                                            } else if (checkPwd && !checkVerifyCode()) {
                                                                //密码正确
                                                                $("#showPassword").addClass("has-success");
                                                                $("#password").attr("aria-describedby", "success");
                                                                $("#success1").show();
                                                                $("#success1").text("");
                                                                $("#success1").append("密码输入正确");
                                                                //验证码错误
                                                                $("#showVerify").addClass("has-error");
                                                                $("#code_input").attr("aria-describedby", "error");
                                                                $("#error2").show();
                                                                $("#error2").text("");
                                                                $("#error2").append("验证码输入错误");

                                                            } else if (!checkPwd && !checkVerifyCode()) {
                                                                //密码错误
                                                                $("#showPassword").addClass("has-error");
                                                                $("#password").attr("aria-describedby", "error");
                                                                $("#error1").show();
                                                                $("#error1").text("");
                                                                $("#error1").append("密码输入错误");
                                                                //验证码错误
                                                                $("#showVerify").addClass("has-error");
                                                                $("#code_input").attr("aria-describedby", "error");
                                                                $("#error2").show();
                                                                $("#error2").text("");
                                                                $("#error2").append("验证码输入错误");
                                                            }

                                                        }
                                                    )
                                                }
                                            }
                                        });


                                    });



                                }

                            }
                        )
                    }
                }
            })
        }


        /*$("#userNum").remove();
        document.getElementById("showIdentity").innerHTML = "<input type='text' class='form-control' id='userNum' placeholder='请输入账号'>";*/

    });





    /*获取userNum*/
    function getUserNum() {
        return document.getElementById("userNum").value.toString();
    }

});







